package app.beit.finportal.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import app.beit.finportal.bean.WebboardBean;
import app.beit.finportal.service.WebboardReplyService;
import app.beit.finportal.service.WebboardService;
import app.beit.finportal.utils.constr.MsgConstr;
import app.beit.finportal.utils.constr.SessionConstr;

@Controller
@RequestMapping("/webboard")
public class WebboardController extends BaseController {
	
	
	private final Logger log = Logger.getLogger(this.getClass());
	
	private final String PAGE = "webboard";
	
	@Autowired
	private WebboardService webboardService;
	
	@Autowired
	private WebboardReplyService webboardReplyService;
	
	@RequestMapping
	public String index(final HttpServletRequest request) throws Exception{
		super.startProcess(request);
		//super.removeAllSession(request);
		request.setAttribute(SessionConstr.ACTIVE_PAGE, PAGE);
		
		try{
			final List<WebboardBean> beans = this.webboardService.getAllPost();
			if(beans != null && beans.size() > 0){
				request.setAttribute(SessionConstr.WEBBOARD_ALL_DATA, beans);
			}
		}catch(Exception ex){
			this.log.error(ex.getMessage());
			ex.printStackTrace();
		}
		
		return "webboard";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value="postWebboard", method = RequestMethod.POST)
	public @ResponseBody FintrackResponse<WebboardBean> postWebboard(final HttpServletRequest request, final @RequestParam("topicName") String topicName
			, final @RequestParam("content") String content, final @RequestParam("postBy") String postBy){
		FintrackResponse<WebboardBean> response = null;
		try{			
				this.log.info("topicName : "+topicName);
				final WebboardBean bean = new WebboardBean();
				
				bean.setName(topicName);
				bean.setContent(content);
				bean.setViewed(0);
				bean.setReplied(0);
				
				bean.setCreatedBy(postBy);
				bean.setCreatedDate(new Date(System.currentTimeMillis()));
				
				
			
				this.webboardService.add(bean);
				response = super.responseSuccess();
				response.setDatas(bean);
				
		}catch(Exception e){
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
			e.printStackTrace();
		}
		return response;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="replyWebboard", method = RequestMethod.POST)
	public String replyWebboard(final HttpServletRequest request, final @RequestParam("webboard_id") String id, final @RequestParam("content") String content, final @RequestParam("postBy") String postBy){
		FintrackResponse<WebboardBean> response = null;
		try{			
				final WebboardBean bean = new WebboardBean();
				
				bean.setWebboardId(Long.parseLong(id));
				bean.setContent(content);
				bean.setCreatedBy(postBy);
				bean.setIsAdmin('N');
				bean.setCreatedDate(new Date(System.currentTimeMillis()));
				
				
			
				this.webboardReplyService.add(bean);
				response = super.responseSuccess();
				response.setDatas(bean);
				
		}catch(Exception e){
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
			e.printStackTrace();
		}
		return "redirect:";
	}
	

	@RequestMapping("/{id}")
	public String details(final HttpServletRequest request, final @PathVariable("id") String id){
		//super.removeAllSession(request);
		request.setAttribute(SessionConstr.ACTIVE_PAGE, PAGE);
		
		try{
			super.startProcess(request);
			final WebboardBean bean = this.webboardService.getWebboardById(id);
			if(bean != null){
				request.setAttribute(SessionConstr.WEBBOARD_ALL_DATA, bean);
				bean.setViewed(bean.getViewed()+1);
				this.webboardService.update(bean);
			}
			
		}catch(Exception ex){
			this.log.error(ex.getMessage());
			ex.printStackTrace();
		}
		
		return "webboard.detail";
	}
	
}
